package com.bluegalaxy.nhb.snapi.controller.api.ck;

import com.alibaba.fastjson.JSONObject;
import com.bluegalaxy.nhb.snservice.common.utils.StringUtil;
import com.bluegalaxy.nhb.snservice.domain.constant.AppConstant;
import com.bluegalaxy.nhb.snservice.domain.vo.api.R;
import com.bluegalaxy.nhb.snservice.domain.vo.api.ck.*;
import com.bluegalaxy.nhb.snservice.service.ck.EventService;
import com.bluegalaxy.nhb.snservice.service.ck.UserGroupService;
import com.bluegalaxy.nhb.snservice.service.ck.UsersService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 用户
 */
@RestController
@RequestMapping(AppConstant.APPLICATION_NAME+"/user")
@Api(value = "用户", tags = "用户API")
public class UsersController {
    private static Log log = LogFactory.getLog(UsersController.class);

    @Autowired
    private UsersService usersService;

    @Autowired
    private EventService eventService;



    @ResponseBody
    @PostMapping("/info")
    @ApiOperation(value = "查询用户基本信息", notes = "查询用户基本信息")
    public R selectInfo(@RequestBody UserParam paramVO) {
        log.info("selectInfo:"+JSONObject.toJSONString(paramVO));
        Map<String,Object> res = new HashMap<>();
        if (StringUtil.isNotEmpty(paramVO.getDistinctId())){
            res = usersService.selectByDistinctId(paramVO.getDistinctId());
        }else if(StringUtil.isNotEmpty(paramVO.getLoginId())){
            res = usersService.selectByLoginId(paramVO.getLoginId());
        }
        if (res != null && !res.isEmpty()){
            res.put("distinct_id",paramVO.getDistinctId());
        }
        return R.data(res);
    }

    @ResponseBody
    @PostMapping("/track")
    @ApiOperation(value = "查询用户轨迹信息", notes = "查询用户轨迹信息")
    public R selectTrack(@RequestBody EventsVO paramVO) {
        List<UserTrackVO> res = eventService.selectTrack(paramVO);
//        if (StringUtil.isNotEmpty(paramVO.getDistinctId())){
//            res = eventService.selectTrackByDistinctId(paramVO.getDistinctId());
//        }else if(StringUtil.isNotEmpty(paramVO.getLoginId())){
//            res = eventService.selectTrackByLoginId(paramVO.getLoginId());
//        }
        return R.data(res);
    }

    @ResponseBody
    @PostMapping("/track/group")
    @ApiOperation(value = "查询用户轨迹信息", notes = "查询用户轨迹信息")
    public R selectTrackGroup(@RequestBody EventsVO paramVO) {
        List<UserTrackGroupVO> resList = new ArrayList<>();

        List<UserTrackVO> res = eventService.selectTrack(paramVO);
        if (!res.isEmpty()){
            Map<String, List<UserTrackVO>> trackMap = res.stream().collect(Collectors.groupingBy(a -> a.getDaily()));
            for (Map.Entry<String, List<UserTrackVO>> m : trackMap.entrySet()) {
                UserTrackGroupVO vo = new UserTrackGroupVO();
                vo.setDaily(m.getKey());
                vo.setTrackList(m.getValue());
                resList.add(vo);
            }
        }

        return R.data(resList);
    }

    @ResponseBody
    @PostMapping("/event/item")
    @ApiOperation(value = "查询用户事件属性", notes = "查询用户事件属性")
    public R selectEventItemList(@RequestBody EventItemParamVO paramVO) {
        log.info("selectEventItemList:"+JSONObject.toJSONString(paramVO));
        List<EventItemVO> resList = new ArrayList<>();
        List<EventItemVO> itemList =paramVO.getItemList();
        List<Map<String,Object>> res = eventService.selectEventItemList(paramVO);
        if (!res.isEmpty()){
            Map<String,Object> map = res.get(0);

            for (Map.Entry<String, Object> m : map.entrySet()) {
                EventItemVO vo = new EventItemVO();
                String itemName = getItemName(m.getKey(),itemList);
                vo.setEventItemName(itemName==null?m.getKey():itemName);
                vo.setEventItemValue(StringUtil.valueOf(m.getValue()));
                resList.add(vo);
            }
        }

        return R.data(resList);
    }

    public static String getItemName(String itemCode,List<EventItemVO> itemList){
        Optional<EventItemVO> optional = itemList.stream().filter(property -> property.getEventItemCode().equals(itemCode)).findFirst();
        if (optional.isPresent()) {
            // 存在
            EventItemVO itemVO =  optional.get();
            return itemVO.getEventItemName();
        } else {
            // 不存在
        }
        return null;
    }

}
